package cn.bsb.controller.backstage;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;

import cn.bsb.pojo.BackendUser;
import cn.bsb.service.backstage.backendUserService;

/**
 * 管理员管理
 */
@Controller
@RequestMapping(value = "admin")
public class AdminController {
	
	@Resource
	private backendUserService backendUser;
	
	@RequestMapping("list")
	public String adminList(Model model,
			@RequestParam(value="pageNum",required=false,defaultValue="1") int pageNum) {
		PageInfo<BackendUser> page = backendUser.getList(pageNum, 10);
		model.addAttribute("page", page);
		List<BackendUser> list = backendUser.getList();
		model.addAttribute("list", list);
		return "backstage/useradmin";
	}
	
	@RequestMapping("selece")
	public String adminGet(Model model,
			@RequestParam("name") Object name,
			@RequestParam(value="pageNum",required=false,defaultValue="1") int pageNum) {
		PageInfo<BackendUser> page = backendUser.getName(pageNum, 10, (String)name);
		model.addAttribute("page", page);
		List<BackendUser> list = backendUser.getList();
		model.addAttribute("list", list);
		model.addAttribute("name", name);
		return "backstage/useradmin";
	}
	
	/*
	 * 删除普通管理员
	 */
	@RequestMapping(value="/deluser/{id}",method=RequestMethod.GET)
	public String deluser(@PathVariable String id){
				try {
					if(backendUser.deleteUserById(Integer.parseInt(id)));
					return "redirect:/admin/list";
				} catch (Exception e) {
					e.printStackTrace();
				}
		return "backstage/useradmin";
	}
	
	/*
	 * 添加管理员
	 */
	@RequestMapping(value="add",method=RequestMethod.GET)
	public String adduser() {
		return "backstage/adminAdd";
	}
	
	
	@RequestMapping(value="adduser",method=RequestMethod.POST)
	public String add(BackendUser user,HttpSession session,HttpServletRequest request,
			 @RequestParam("a_idPicPath") MultipartFile attach) {
		String idPicPath = null;
		if(!attach.isEmpty()) {
			String datePath = "statics"+File.separator+"uploadfiles" ;
			String path = request.getSession().getServletContext().getRealPath(datePath);
			String old = attach.getOriginalFilename();//原文件名
			String prefix = FilenameUtils.getExtension(old);//源文件后缀
			int filesize = 5000000;
			if(attach.getSize() > filesize) {
            	request.setAttribute("errorInfo", " * 上传大小不得超过 500k");
            	return "backstage/adminAdd";
			}else if(prefix.equalsIgnoreCase("jpg") || prefix.equalsIgnoreCase("png") 
	            		|| prefix.equalsIgnoreCase("jpeg") || prefix.equalsIgnoreCase("pneg")){//上传图片格式不正确
				String fileName = System.currentTimeMillis()+RandomUtils.nextInt(1000000)+"_Personal.jpg";
				File targetFile = new File(path,fileName);
				if(!targetFile.exists()){  
                    targetFile.mkdirs();  
                } 
				//保存  
	             try {
					attach.transferTo(targetFile);  
	            } catch (Exception e) {  
	                e.printStackTrace();  
	                request.setAttribute("errorInfo"," * 上传失败！");
	                return "backstage/adminAdd";
	            }  
	             idPicPath = datePath+File.separator+fileName;
			}else {
				request.setAttribute("errorInfo"," * 上传图片格式不正确");
				return "backstage/adminAdd";
			}
		}
		user.setCreattime(new Date());
		user.setPic(idPicPath);
		try {
			if(backendUser.add(user)) {
				return "redirect:/admin/list";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "backstage/adminAdd";
	}
	
}
